﻿//**********
//save place, load place
//**********

function loadPlace(placeName, gameToken, defaultplaceItemJson) {
  var placeJson = readCookie(gameToken + placeName);
  if (!placeJson) {
    placeJson = defaultplaceItemJson;
  }
  alert(placeJson);
  //if it doesn't start with 'place = ' then append it now...
  placeJson = "place = " + placeJson;
  return eval(placeJson);
}

function savePlace(placeName, gameToken, place) {
  var placeJson = "place = " + toJson(place);
  createCookie(gameToken + placeName, placeJson, 0);
}


//platform function:
//apply an action to a noun.
//Routing!
function Apply (action, noun) 
{	
	var i = 0;
	var params = new Array(); 
	if (typeof place[noun][action] == 'object') {
		for (var property in place[noun][action]) {
			if (i == 0) {
				func = property;
			} else { 
			params.push(place[noun][action][property]);
			}
			i++	;
		}
		place[noun][action][func].apply(place, params);
		return true;				
	}
  
  if (!place[noun]) {
	write('Not sure what is meant by ' + noun, noun + '_' + action);
	return false;
  }
  
  action = action.toLowerCase().split(" ")[0];		  
  
  if (!place[noun][action]) {
    write('You try to ' + action + ' the ' + noun + ' but think better of it.', noun + '_' + action);
	return false;
  }
  
  if (!place[noun][action].prototype) {
    //it's a string, so write it. (note: could use 'typeof' instead?)
    write(place[noun][action], noun + '_' + action);
    return true;
  }
  
  place[noun][action]();
  return true;
}
